package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Cutter;
import com.qf.service.CutterService;
import com.qf.util.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 刀具表(Cutter)表控制层！！！！！！！111111
 *  制造冲突  当前控制类 是 关于刀具相关业务------制造冲突22222222222 --1 制造冲突
 * @author makejava  ------制造冲突22222222222 --1 制造冲突
 * @since 2023-09-22 20:49:41
 */
@RestController
@RequestMapping("/api/cutter")
public class CutterController {
    /**
     * 服务对象
     */
    @Autowired
    private CutterService cutterService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @PostMapping("/list/page")
    public R queryByPage(@RequestParam Map<String,String> map) {
        String pageNum = map.get("pageNum");
        String pageSize = map.get("pageSize");
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        List<Cutter> all = cutterService.queryByPage(map);
        PageInfo<Cutter> info = new PageInfo<>(all);
        return R.ok(info);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public R queryById(@PathVariable("id") Integer id) {
        return R.ok(this.cutterService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param cutter 实体
     * @return 新增结果
     */
    @PostMapping("/save")
    public R add(Cutter cutter) {
        cutter.setCutterState("是");
        cutter.setCreateTime(new Date());
        int insert = cutterService.insert(cutter);
        if (insert > 0) {
            return R.ok(insert);
        }
        return R.fail();
    }

    /**
     * 编辑数据
     *
     * @param cutter 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public R edit( Cutter cutter ) {
        int update = cutterService.update(cutter);
        if (update > 0){
            return R.ok(update);
        }
        return R.fail();
    }

    /**
     * 删除数据
     *
     * @param cutter 实体
     * @return 删除是否成功
     */
    @PostMapping("/del")
    public R deleteById( Cutter cutter) {
        System.out.println(cutter);
        int id = cutter.getCutterId();
        boolean b = cutterService.deleteById(id);
        if (b) {
            return R.ok(true);
        }
        return R.fail();
    }

    /**
     * 批量删除
     *
     * @param  list
     * @return 删除是否成功
     */

    @PostMapping("/delSelection")
    public R deleteSelection(@RequestParam ArrayList list) {
        System.out.println(list);
        int i = cutterService.deleteSelection(list);
        return i>0? R.ok(i):R.fail();
    }


}

